<template>
    <div class="app-container">
        <div class="mb8" v-if="$route.query.planName">
            <el-alert
                :title="'当前招聘计划: ' + $route.query.planName"
                type="info"
                :closable="false"
                show-icon
            >
            </el-alert>
        </div>

        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="部门" prop="deptId">
                <treeselect
                    v-model="queryParams.deptId"
                    :options="deptOptions"
                    :show-count="true"
                    placeholder="请选择部门"
                    @keyup.enter.native="handleQuery"
                    style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="岗位" prop="postId">
                <el-select
                    v-model="queryParams.postId"
                    placeholder="请选择岗位"
                    clearable
                >
                    <el-option
                        v-for="dict in postOptions"
                        :key="dict.postId"
                        :label="dict.postName"
                        :value="dict.postId"
                    />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['hrm:position:add']"
                        >新增</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['hrm:position:edit']"
                        >修改</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['hrm:position:remove']"
                        >删除</el-button
                    >
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['hrm:position:export']"
                        >导出</el-button
                    >
                </el-col>
                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="positionList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="标题" align="center" prop="title" />
                <el-table-column
                    label="内容"
                    align="center"
                    prop="content"
                    :show-overflow-tooltip="true"
                />
                <el-table-column label="部门" align="center" prop="deptName" />
                <el-table-column label="岗位" align="center" prop="postName" />

                <el-table-column
                    label="创建时间"
                    align="center"
                    prop="createTime"
                    width="180"
                >
                    <template slot-scope="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="handleView(scope.row)"
                            v-hasPermi="['hrm:position:query']"
                            >查看候选人</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['hrm:position:edit']"
                            >修改</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['hrm:position:remove']"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改岗位需求对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-form
                    ref="form"
                    :model="form"
                    :rules="rules"
                    label-width="120px"
                >
                    <el-form-item label="标题" prop="title">
                        <el-input
                            v-model="form.title"
                            placeholder="请输入标题"
                        />
                    </el-form-item>
                    <el-form-item label="内容">
                        <editor v-model="form.content" :min-height="192" />
                    </el-form-item>
                    <el-form-item label="部门" prop="deptId">
                        <treeselect
                            v-model="form.deptId"
                            :options="deptOptions"
                            :show-count="true"
                            placeholder="请选择部门"
                            @select="handleDeptSelect"
                        />
                    </el-form-item>
                    <el-form-item label="岗位" prop="postId">
                        <el-select
                            v-model="form.postId"
                            placeholder="请选择岗位"
                        >
                            <el-option
                                v-for="dict in postOptions"
                                :key="dict.postId"
                                :label="dict.postName"
                                :value="dict.postId"
                            />
                        </el-select>
                    </el-form-item>
                    <el-divider content-position="center"
                        >招聘岗位候选人信息</el-divider
                    >
                    <el-row :gutter="10" class="mb8">
                        <el-col :span="1.5">
                            <el-button
                                type="primary"
                                icon="el-icon-plus"
                                size="mini"
                                @click="handleAddHrmRecruitmentCandidate"
                                >添加</el-button
                            >
                        </el-col>
                        <el-col :span="1.5">
                            <el-button
                                type="danger"
                                icon="el-icon-delete"
                                size="mini"
                                @click="handleDeleteHrmRecruitmentCandidate"
                                >删除</el-button
                            >
                        </el-col>
                    </el-row>
                    <el-table
                        :border="Global.tableShowBorder"
                        :data="hrmRecruitmentCandidateList"
                        :row-class-name="rowHrmRecruitmentCandidateIndex"
                        @selection-change="
                            handleHrmRecruitmentCandidateSelectionChange
                        "
                        ref="hrmRecruitmentCandidate"
                    >
                        <el-table-column
                            type="selection"
                            width="50"
                            align="center"
                        />
                        <el-table-column
                            label="序号"
                            align="center"
                            prop="index"
                            width="50"
                        />
                        <el-table-column label="姓名" prop="name" width="150">
                            <template slot-scope="scope">
                                <el-input
                                    v-model="scope.row.name"
                                    placeholder="请输入姓名"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column label="年龄" prop="age" width="150">
                            <template slot-scope="scope">
                                <el-input
                                    v-model="scope.row.age"
                                    placeholder="请输入年龄"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column label="性别" prop="sex" width="150">
                            <template slot-scope="scope">
                                <el-select
                                    v-model="scope.row.sex"
                                    placeholder="请选择性别"
                                >
                                    <el-option label="男" value="1" />
                                    <el-option label="女" value="2" />
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="最高学历"
                            prop="education"
                            width="150"
                        >
                            <template slot-scope="scope">
                                <el-select
                                    v-model="scope.row.education"
                                    placeholder="请选择学历"
                                >
                                    <el-option label="高中" value="高中" />
                                    <el-option label="专科" value="专科" />
                                    <el-option label="本科" value="本科" />
                                    <el-option label="硕士" value="硕士" />
                                    <el-option label="博士" value="博士" />
                                </el-select>
                            </template>
                        </el-table-column>
                        <el-table-column label="电话" prop="tel" width="150">
                            <template slot-scope="scope">
                                <el-input
                                    v-model="scope.row.tel"
                                    placeholder="请输入电话"
                                />
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="电子邮件"
                            prop="email"
                            width="150"
                        >
                            <template slot-scope="scope">
                                <el-input
                                    v-model="scope.row.email"
                                    placeholder="请输入电子邮件"
                                />
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 查看岗位需求对话框 -->
        <el-dialog
            title="查看岗位需求"
            :visible.sync="openView"
            width="1200px"
            append-to-body
        >
            <TableCard>
                <el-descriptions :column="2" border>
                    <el-descriptions-item label="标题">{{
                        form.title
                    }}</el-descriptions-item>
                    <el-descriptions-item label="部门">{{
                        form.deptName
                    }}</el-descriptions-item>
                    <el-descriptions-item label="岗位">{{
                        form.postName
                    }}</el-descriptions-item>
                    <el-descriptions-item label="创建时间">{{
                        parseTime(form.createTime)
                    }}</el-descriptions-item>
                    <el-descriptions-item label="内容" :span="2">
                        <div v-html="form.content"></div>
                    </el-descriptions-item>
                </el-descriptions>

                <el-divider content-position="center"
                    >招聘岗位候选人信息</el-divider
                >

                <el-table
                    :border="Global.tableShowBorder"
                    :data="hrmRecruitmentCandidateList"
                >
                    <el-table-column
                        label="序号"
                        type="index"
                        width="50"
                        align="center"
                    />
                    <el-table-column label="姓名" prop="name" align="center" />
                    <el-table-column label="年龄" prop="age" align="center" />
                    <el-table-column label="性别" prop="sex" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.sex === "1" ? "男" : "女" }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="最高学历"
                        prop="education"
                        align="center"
                    />
                    <el-table-column label="电话" prop="tel" align="center" />
                    <el-table-column
                        label="电子邮件"
                        prop="email"
                        align="center"
                    />
                </el-table>
            </TableCard>
            <div slot="footer" class="dialog-footer">
                <el-button @click="openView = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listPosition,
    getPosition,
    delPosition,
    addPosition,
    updatePosition,
} from "@/api/hrm/position";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";
import { treeselect } from "@/api/system/dept";
import { listPostAll } from "@/api/system/post";

export default {
    name: "Position",
    components: {
        Treeselect,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 子表选中数据
            checkedHrmRecruitmentCandidate: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 岗位需求表格数据
            positionList: [],
            // 招聘岗位候选人表格数据
            hrmRecruitmentCandidateList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否显示查看弹出层
            openView: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                orderByColumn: "createTime",
                isAsc: "descending",
                planId: this.$route.query.planId,
                title: null,
                deptId: null,
                postId: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                title: [
                    {
                        required: true,
                        message: "标题不能为空",
                        trigger: "blur",
                    },
                ],
                deptId: [
                    {
                        required: true,
                        message: "部门不能为空",
                        trigger: "change",
                    },
                ],
                postId: [
                    {
                        required: true,
                        message: "岗位不能为空",
                        trigger: "change",
                    },
                ],
            },
            // 部门树选项
            deptOptions: [],
            deptOptionsArray: null,
            // 岗位选项
            postOptions: [],
        };
    },
    created() {
        this.getList();
        this.getTreeselect();
        this.getPostList();
    },
    methods: {
        /** 查询部门下拉树结构 */
        getTreeselect() {
            treeselect().then((response) => {
                this.deptOptions = response.data;
                this.deptOptionsArray = this.flattenArray(this.deptOptions);
            });
        },
        flattenArray(arr) {
            let result = [];
            for (let i = 0; i < arr.length; i++) {
                let item = arr[i];
                result.push(item);
                if (item.children) {
                    result = result.concat(this.flattenArray(item.children));
                }
            }
            return result;
        },
        parseSelectTree(id) {
            let tempObj = this.deptOptionsArray.find((item) => item.id == id);
            if (tempObj) {
                return tempObj.label;
            }
            return "—";
        },
        /** 获取岗位列表 */
        getPostList() {
            listPostAll().then((res) => {
                this.postOptions = res.data;
            });
        },
        parsePostName(ids) {
            if (!ids) {
                return "—";
            }
            if (ids != null && ids != "") {
                let obj = this.postOptions.find((item) => item.postId == ids);
                if (obj) {
                    return obj.postName;
                }
            }
            return "—";
        },
        /** 查询岗位需求列表 */
        getList() {
            this.loading = true;
            listPosition(this.queryParams).then((response) => {
                this.positionList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                planId: this.$route.query.planId,
                title: null,
                content: null,
                deptId: null,
                deptName: null,
                postId: null,
                postName: null,
            };
            this.hrmRecruitmentCandidateList = [];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.planId = this.$route.query.planId;
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加岗位需求";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getPosition(id).then((response) => {
                this.form = response.data;
                this.hrmRecruitmentCandidateList =
                    response.data.hrmRecruitmentCandidateList;
                this.open = true;
                this.title = "修改岗位需求";
            });
        },
        /** 查看按钮操作 */
        handleView(row) {
            this.reset();
            const id = row.id;
            getPosition(id).then((response) => {
                this.form = response.data;
                this.hrmRecruitmentCandidateList =
                    response.data.hrmRecruitmentCandidateList;
                this.openView = true;
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    this.form.hrmRecruitmentCandidateList =
                        this.hrmRecruitmentCandidateList;
                    this.form.deptName = this.parseSelectTree(this.form.deptId);
                    this.form.postName = this.parsePostName(this.form.postId);
                    if (this.form.id != null) {
                        updatePosition(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addPosition(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm('是否确认删除岗位需求编号为"' + ids + '"的数据项？')
                .then(function () {
                    return delPosition(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 招聘岗位候选人序号 */
        rowHrmRecruitmentCandidateIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
        },
        /** 招聘岗位候选人添加按钮操作 */
        handleAddHrmRecruitmentCandidate() {
            let obj = {};
            obj.name = "";
            obj.age = "";
            obj.sex = "";
            obj.education = "";
            obj.tel = "";
            obj.email = "";
            this.hrmRecruitmentCandidateList.push(obj);
        },
        /** 招聘岗位候选人删除按钮操作 */
        handleDeleteHrmRecruitmentCandidate() {
            if (this.checkedHrmRecruitmentCandidate.length == 0) {
                this.$modal.msgError("请先选择要删除的招聘岗位候选人数据");
            } else {
                const hrmRecruitmentCandidateList =
                    this.hrmRecruitmentCandidateList;
                const checkedHrmRecruitmentCandidate =
                    this.checkedHrmRecruitmentCandidate;
                this.hrmRecruitmentCandidateList =
                    hrmRecruitmentCandidateList.filter(function (item) {
                        return (
                            checkedHrmRecruitmentCandidate.indexOf(
                                item.index
                            ) == -1
                        );
                    });
            }
        },
        /** 复选框选中数据 */
        handleHrmRecruitmentCandidateSelectionChange(selection) {
            this.checkedHrmRecruitmentCandidate = selection.map(
                (item) => item.index
            );
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/position/export",
                {
                    ...this.queryParams,
                },
                `position_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
